package org.example;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class getNews {
	//人民日报
	public final String people = "http://society.people.com.cn/GB/136657/index.html";
	public List<NewsBody> people() throws IOException {
		Document jsoup = Jsoup.connect(people).get();
		Elements elements = jsoup.select("body li >a");
		List<NewsBody> list = new ArrayList<NewsBody>();
		int count = elements.size()>=10?10:elements.size();
		for(int i = 0;i< count;i++) {
			try {
				Element element = elements.get(i);
				NewsBody newsBody = new NewsBody();
				newsBody.head = element.text();
				newsBody.Url = "http://society.people.com.cn"+element.attr("href");
				Document jsoupTemp = Jsoup.connect(newsBody.Url).get();
				Elements elementsTemp = jsoupTemp.select("body p");
				newsBody.firstP = elementsTemp.get(0).text()+"\n";
				newsBody.firstP += null!=elementsTemp.get(1).text()?elementsTemp.get(1).text():"";
				list.add(newsBody);
			}catch (Exception e) {
			}
		}
		return list;
	}

	//环球网
	public final String huanqiu = "https://china.huanqiu.com/api/list";
	public List<NewsBody> huanqiu() throws IOException {
		String s = HttpRequest.sendGet(huanqiu, "node=%22/e3pmh1nnq/e3pmh1obd%22,%22/e3pmh1nnq/e3pn61c2g%22,%22/e3pmh1nnq/e3pn6eiep%22,%22/e3pmh1nnq/e3pra70uk%22,%22/e3pmh1nnq/e5anm31jb%22,%22/e3pmh1nnq/e7tl4e309%22&offset=0&limit=20");
		JSONArray ja = JSONObject.fromObject(s).getJSONArray("list");
		List<NewsBody> list = new ArrayList<NewsBody>();
		int count = ja.size()>=10?10:ja.size();
		for(int i = 0;i< count;i++) {
			try {
				JSONObject jo = (JSONObject) ja.get(i);
				NewsBody newsBody = new NewsBody();
				newsBody.head = jo.getString("title");
				newsBody.Url = "https://china.huanqiu.com/article/"+ jo.getString("aid");
				newsBody.firstP = jo.getString("summary");
				list.add(newsBody);
			}catch (Exception e) {
			}
		}
		return list;
	}

	//新浪新闻
	public final String sina = "https://news.sina.com.cn/china/";
	public List<NewsBody> sina() throws IOException {
		String s = HttpRequest.sendGet(sina, "");
		Document jsoup = Jsoup.parse(s);
		Elements elements = jsoup.select("div.left-content-1 ul> li>  a");
		List<NewsBody> list = new ArrayList<NewsBody>();
		int count = elements.size() >= 20 ? 20 : elements.size();
		for (int i = 0; i < count; i++) {
			try {
				Element element = elements.get(i);
				NewsBody newsBody = new NewsBody();
				newsBody.head = element.text();
				newsBody.Url = element.attr("href");
				Document jsoupTemp = Jsoup.connect(newsBody.Url).get();
				Elements elementsTemp = jsoupTemp.select("#article > p");
				newsBody.firstP = null != elementsTemp.get(1).text() ? elementsTemp.get(1).text() : "";
				if(elementsTemp.size()>=3) {
					newsBody.firstP += null != elementsTemp.get(2).text() ? elementsTemp.get(2).text() : "";
				}
				list.add(newsBody);
			}catch (Exception e) {
			}
		}
		return list;
	}
	//澎湃新闻
	public final String thepaper = "https://www.thepaper.cn/";
	public List<NewsBody> thepaper() throws IOException {
		String s = HttpRequest.sendGet(thepaper,"");
		Document jsoup = Jsoup.parse(s);
		Elements elements = jsoup.select("#indexScroll h2 a");
		List<NewsBody> list = new ArrayList<NewsBody>();
		int count = elements.size()>=20?20:elements.size();
		for(int i = 0;i< count;i++) {
			try {
				Element element = elements.get(i);
				NewsBody newsBody = new NewsBody();
				newsBody.head = element.text();
				newsBody.Url = "https://www.thepaper.cn/"+element.attr("href");
				Document jsoupTemp = Jsoup.connect(newsBody.Url).get();
				Element elementsTemp = jsoupTemp.selectFirst("div.news_txt");
				if(elementsTemp!=null) {
					String text = elementsTemp.text();
					newsBody.firstP = text.substring(0, text.indexOf("。")+1)+"\n";
					text = text.substring(text.indexOf("。")+1);
					newsBody.firstP += text.substring(0, text.indexOf("。")+1);
					list.add(newsBody);
				}
			}catch (Exception e) {
			}
		}
		return list;
	}


	//光明网 教育科技文化
	public final String gmwEdu = "https://news.gmw.cn/node_23707.htm";
	//光明网 经济
	public final String gmwFin = "https://news.gmw.cn/node_23545.htm";
	//光明网 法治社会
	public final String gmwLaw = "https://news.gmw.cn/node_23708.htm";
	public List<NewsBody> gmw(String url) throws IOException {
		Document jsoup = Jsoup.connect(url).get();
		Elements elements = jsoup.select(".channel-newsGroup > li");
		List<NewsBody> list = new ArrayList<NewsBody>();
		for(int i = 0;i< elements.size();i++) {
			try {
				Element element = elements.get(i);
				String DATE_FORMAT = "yyyy-MM-dd";
		        LocalDate localDate = LocalDate.now();
		        String time = localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
				if(!element.select(".channel-newsTime").text().equals(time)) {
					break;
				}
				element = element.selectFirst("a");
				NewsBody newsBody = new NewsBody();
				newsBody.head = element.text();
				if(!element.attr("href").substring(0,4).equals("http")) {
					newsBody.Url = "https://news.gmw.cn/"+element.attr("href");
				}else {
					newsBody.Url = element.attr("href");
				}
				Document jsoupTemp = Jsoup.connect(newsBody.Url).get();
				Elements elements2 = jsoupTemp.select(" div.u-mainText > p");
				newsBody.firstP = elements2.get(0).text();
				if(elements2.size()>=2) {
					newsBody.firstP += "\n"+elements2.get(1).text();
				}
				list.add(newsBody);
			}catch (Exception e) {
			}
		}
		return list;
	}







	public final String toutiaoUrl = "https://www.toutiao.com/hot-event/hot-board";
	public List<NewsBody> toutiao() throws IOException {
		String s=HttpRequest.sendGet(toutiaoUrl, "origin=toutiao_pc");
		JSONArray ja = JSONObject.fromObject(s).getJSONArray("data");
		List<NewsBody> list = new ArrayList<NewsBody>();
		int count = ja.size()>=10?10:ja.size();
		for(int i = 0;i< count;i++) {
			try {
				NewsBody newsBody = new NewsBody();
				JSONObject jObject = ja.getJSONObject(i);
				newsBody.head = jObject.getString("Title");
				String url = jObject.getString("Url");
	
				//WebClient wc = new WebClient(1);
				s=HttpRequest.sendGet(url.split("\\?")[0],url.split("\\?")[1]);
				Document doc= Jsoup.parse(s);
			    Elements e=doc.select("div.abstract-container > a");
			    newsBody.Url = e.attr("href");
			}catch (Exception e) {
			}
		}
		return null;
	}
}
